import { defineStore } from "pinia";
import { getUserDetailData } from "@/services/indexManagement.service";
import { getProvList } from "@/services/userManage.service";
export const userDetail = defineStore("user-detail", {
  state: () => {
    return {
      searchParams: {
        month: "",
        startDate: "",
        endDate: "",
        userName: "",
        realName: "",
        mail: "",
        provName: undefined,
        cityName: undefined,
        dialogFlag: undefined,
        title: "",
        tenantId: "",
        tenantName: "",
      },
      pageNo: 1,
      pageSize: 15,
      total: 0,
      columns: [
        {
          title: "月份",
          dataIndex: "month",
          align: "center",
          // fixed: "left",
          ellipsis: true,
          // width: 150,
        },
        {
          title: "账期",
          dataIndex: "date",
          align: "center",
          ellipsis: true,
          width: 200,
        },
        {
          title: "账号",
          dataIndex: "userName",
          align: "center",
          ellipsis: true,
        },
        {
          title: "姓名",
          dataIndex: "realName",
          align: "center",
          ellipsis: true,
        },
        {
          title: "mail",
          dataIndex: "mail",
          align: "center",
          ellipsis: true,
        },
        {
          title: "省分",
          dataIndex: "provName",
          align: "center",
          ellipsis: true,
        },
        {
          title: "地市",
          dataIndex: "cityName",
          align: "center",
          ellipsis: true,
          scopedSlots: { customRender: "cityName" },
        },
        {
          title: "取数菜单",
          dataIndex: "dialogFlag",
          align: "center",
          ellipsis: true,
          scopedSlots: { customRender: "dialogFlag" },
        },
        {
          title: "用户问题描述",
          dataIndex: "title",
          align: "center",
          ellipsis: true,
          width: 250,
        },
        {
          title: "租户ID",
          dataIndex: "tenantId",
          align: "center",
          ellipsis: true,
        },
        {
          title: "租户名称",
          dataIndex: "tenantName",
          align: "center",
          ellipsis: true,
        },
        {
          title: "操作",
          dataIndex: "operate",
          align: "center",
          fixed: "right",
          scopedSlots: { customRender: "operate" },
        },
      ],
      tableData: [],
      provOption: [],
    };
  },
  getters: {
    cityOption() {
      let arr = [];
      if (
        !this.searchParams.provName ||
        this.searchParams.provName === "全国" ||
        this.provOption.length === 0
      ) {
        arr = [];
      } else {
        arr = this.provOption.find(
          (item) => item.title === this.searchParams.provName
        ).children;
      }
      return arr;
    },
  },
  actions: {
    async getRegion() {
      const res = await getProvList();
      if (res && res?.code === 0) {
        this.provOption = res.result[0].children;
        this.provOption.unshift({
          title: res.result[0].title,
          key: res.result[0].key,
        });
      } else {
        this.provOption = [];
      }
    },
    async reSet() {
      this.searchParams = {
        month: "",
        startDate: "",
        endDate: "",
        userName: "",
        realName: "",
        mail: "",
        provName: undefined,
        cityName: undefined,
        dialogFlag: undefined,
        title: "",
        tenantId: "",
        tenantName: "",
      };
      if (this.pageNo === 1) {
        await this.getTbodyData();
      } else {
        this.pageNo = 1;
      }
    },
    isValidJSON(jsonString) {
      if (typeof jsonString !== "string") {
        return false;
      }
      try {
        JSON.parse(jsonString);
        return true;
      } catch (error) {
        return false;
      }
    },
    async getTbodyData() {
      let obj = { ...this.searchParams };
      for (let key in obj) {
        if (!obj[key]) {
          obj[key] = "";
        }
      }
      const res = await getUserDetailData({
        ...obj,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      });
      if (res && res?.code === 200) {
        this.total = res?.result?.total;
        this.tableData =
          (res?.result?.records &&
            res?.result?.records?.map((item) => {
              return {
                ...item,
                title: this.isValidJSON(item.title)
                  ? JSON.parse(item.title)
                  : item.title,
              };
            })) ||
          [];
      } else {
        this.total = 0;
        this.tableData = [];
      }
    },
    async search() {
      if (this.pageNo === 1) {
        await this.getTbodyData();
      } else {
        this.pageNo = 1;
      }
    },
  },
});
